import datetime
from collections import defaultdict

from common.mssql_helper import g_msql_inst
from common.utils import create_uuid

__author__ = "Ypf"
_date_ = "2019/9/11-16:16"
def time_copy():
    list1 = []
    fist = datetime.date(datetime.date.today().year, datetime.date.today().month - 1, 1)
    last = datetime.date(datetime.date.today().year, datetime.date.today().month, 1) - datetime.timedelta(1)
    for i in range(7):
        list1.append(str(fist))
        list1.append(str(last))
    value_tuple = tuple(list1)
    return value_tuple


class Zjzhmx(object):
    @staticmethod
    def get_zjzhmx(sql):
        touple=time_copy()
        print (touple)
        end_sql=sql%touple
        res_list1 = g_msql_inst.fetch_all(end_sql)
        dic = defaultdict(dict)
        for (sname,sqbz,qcje,sysr,syzr,syflk,syzc,glfy,khfy,cpfy) in res_list1:
            list1 = []
            if sqbz =='402881d814a4c7120114a505cbea4194' or '402881d814a4c7120114a5058be44192':
                sqbz='人民币'
            elif sqbz=='402881d814a4c7120114a505cbea4194':
                sqbz='美金'
            elif sqbz=='402881d818c2db620118cf3b4d0a7314':
                sqbz = '港币'
            for i in [sname, sqbz, qcje, sysr, syzr, syflk, syzc, glfy, khfy,cpfy]:
                if i==None:
                    i=0
                    list1.append(i)
                else:
                    list1.append(i)
            kcxj=int(list1[2])+int(list1[3])+int(list1[4])-int(list1[5])-int(list1[6])-int(list1[7])-int(list1[8])-int(list1[9])
            list1.append(kcxj)
            dic[sname]=list1
        return dic

    @staticmethod
    def inser_db(db_insert_sql,dic,time):
        for key in dic:
            id = create_uuid()
            dic[key].append(time + '-01')
            dic[key].append(id)
            value_tuple = tuple(dic[key])
            excuete_sql = db_insert_sql % value_tuple
            g_msql_inst.db_excuete(excuete_sql)